 #include "initialisation.h"

  

   void init(char *title, int *maxW, int *maxH)
   {
  
    
    int video_mode;
    if (SDL_Init(SDL_INIT_VIDEO ) < 0)
    {
        printf("Could not initialize SDL: %s\n", SDL_GetError());
        exit(1);
    }
     FILE* video = NULL;
    video = fopen("menu/options/video.txt", "r");
    if (video != NULL)
    {
        
        fscanf(video, "%d",&video_mode);
        if (video_mode ==1)
        {
            

            jeu.ecran = SDL_SetVideoMode(0, 0, 0, SDL_FULLSCREEN | SDL_HWPALETTE|SDL_DOUBLEBUF); //SDL_FULLSCREEN
            if (jeu.ecran == NULL)
            {
                fprintf(stderr, "Impossible de charger le mode vidéo : %s\n", SDL_GetError());
                exit(EXIT_FAILURE);
            }
            const SDL_VideoInfo *videoInfo;
 
            videoInfo=SDL_GetVideoInfo();
            *maxW=videoInfo->current_w;
            *maxH=videoInfo->current_h;
            
        }

        if (video_mode ==0)
        {
            jeu.ecran = SDL_SetVideoMode(1000, 700, 0, SDL_HWSURFACE | SDL_HWPALETTE|SDL_DOUBLEBUF); //SDL_FULLSCREEN
            if (jeu.ecran == NULL)
             {
               fprintf(stderr, "Impossible de charger le mode vidéo : %s\n", SDL_GetError());
                exit(EXIT_FAILURE);
             }

        }
    fclose(video); // On ferme le fichier qui a été ouvert

     if (TTF_Init() < 0)
    {
        printf("Couldn't initialize SDL TTF: %s\n", SDL_GetError());

        exit(1);
    }

    

    font = loadFont("images/GenBasB.ttf", 32);
    fontMaxi = loadFont("images/GenBasB.ttf", 250);

    /* Initialise SDL_Mixer */

    int flags=MIX_INIT_MP3;
    int initted=Mix_Init(flags);
    

    
    if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)==-1) {
        printf("Mix_OpenAudio: %s\n", Mix_GetError());
        exit(1);
    }

   
    Mix_AllocateChannels(32);

    }


    

    

    SDL_WM_SetCaption(title, NULL);


    

    SDL_ShowCursor(SDL_DISABLE);




    }





     int SerialRead(char buffer[]) 
    {
     int i=0,len; 
     FILE* arduinoIN = NULL;
    arduinoIN = fopen("/dev/ttyACM0", "r");
    if (arduinoIN != NULL)
    {
   
            len=fgetc(arduinoIN);
             while(i<len) {
              while((buffer[i]=fgetc(arduinoIN))!=EOF) 
                { 
                    i++; 
                } 
            
        } return i; 
      }fclose(arduinoIN);
    }


     void loadGame(void)
    {

        
        map.background = loadImage("images/background.png");
        map.background2 = loadImage("images/mout.png");
        map.tileSet = loadImage("images/level1.png");
        map.backgroundStage2= loadImage("images/background2.jpg");
        map.backgroundStage3=loadImage("lvl3/background3.png");

        loadMap("images/map1.txt");
        jeu.vies = 3;
        jeu.etoiles = 0;
        jeu.level=2;

    
        jeu.HUD_vie = IMG_Load("images/life.png");
        jeu.HUD_etoiles = loadImage("images/stars.png");
        jeu.jaugePlayer = IMG_Load("lvl2/playerlife.png");
        jeu.jaugeEnnemy = IMG_Load("lvl2/ninjalife.png");
        player.HP= IMG_Load("lvl2/jauge.png");
        Ninja.HP= IMG_Load("lvl2/jauge.png");
        loadSong("music/sia.mp3");
        Ninja.monstre1=IMG_Load("images/ninja1.png");
        Ninja.monstre2=IMG_Load("images/ninja2.png");
        Ninja.monstre3=IMG_Load("lvl2/ninja3.png");
        Ninja.monstre4=IMG_Load("lvl2/ninjadead.png");

        loadSound();

     }



   

    void cleanup()
   {
      int i;
     

       if (map.background != NULL)
      {
            SDL_FreeSurface(map.background);

      }

      

      

       if (map.tileSet != NULL)
      {
            SDL_FreeSurface(map.tileSet);
      }

        if (jeu.HUD_etoiles != NULL)
      {
            SDL_FreeSurface(jeu.HUD_etoiles);
      }
      if (jeu.HUD_vie != NULL)
      {
            SDL_FreeSurface(jeu.HUD_vie);
      }

      if ( jeu.musique != NULL )
      Mix_FreeMusic(jeu.musique);
      freeSound();
     
      Mix_CloseAudio();
      Mix_Quit();


      closeFont(font);


      TTF_Quit();


      
      SDL_Quit();

    }
